#include "linux/common.h"
#include "linux/syscall.h"
#include "linux/kernel.h"
#include "linux/error.h"
#include "linux/printk.h"
#include "linux/vfs.h"
#include "linux/string.h"
#include "linux/proc.h"
#include "linux/sched.h"
#include "asm/ptrace.h"


extern int do_sys_fork(void);
extern unsigned long sys_brk(unsigned long brk);
extern struct file *get_empty_filp(void);
extern unsigned long sys_mmap_pgoff(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, unsigned long fd, unsigned long pgoff);

int system_call_dup2(int oldfd, int newfd)
{
    if (newfd > NR_OPEN || oldfd > NR_OPEN)
        return -EBADF;

    if (oldfd == newfd)
        return newfd;
    
    if (!current->filp[newfd])
        current->filp[newfd] = get_empty_filp();
    
    if (!current->filp[newfd])
        return -EBADF;

    if (!current->filp[oldfd])
        return -EBADF;

    memcpy(current->filp[newfd], current->filp[oldfd], sizeof (struct file));
    
    return newfd;
}

void system_call_nouse(void)
{

}

void *sys_call_table[] =
{
	system_call_nouse,
	sys_open,
	sys_read,
	sys_write,
	sys_close,
	sys_mmap_pgoff,
	sys_brk,
	do_sys_fork,
	process_sleep,
	process_msleep,
};
